#!/bin/bash

# 定义要测试的矩阵大小
sizes=(16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 336 352 368 384 400 416 432 448 464 480 496 512)

# 可执行文件路径
executable="./build/bin/opapi_test"

# 设置断点续测的起始位置
# 注意：这里设置的是上次完成的测试，脚本会从下一个测试开始
RESUME_M=96
RESUME_N=224
RESUME_K=80

# 检查可执行文件是否存在
if [ ! -f "$executable" ]; then
    echo "错误: 找不到可执行文件 $executable"
    exit 1
fi

# 计算已完成的测试数
calculate_completed_tests() {
    local count=0
    local found=0
    
    for m in "${sizes[@]}"; do
        for n in "${sizes[@]}"; do
            for k in "${sizes[@]}"; do
                if [ $m -eq $RESUME_M ] && [ $n -eq $RESUME_N ] && [ $k -eq $RESUME_K ]; then
                    found=1
                    count=$((count + 1))
                    return $count
                fi
                count=$((count + 1))
            done
        done
    done
    
    # 如果没找到，返回0
    return 0
}

# 计算总的测试组合数
total_combinations=$((${#sizes[@]} * ${#sizes[@]} * ${#sizes[@]}))

# 计算已完成的测试数
calculate_completed_tests
completed_tests=$?

# 打印开始信息
echo "========================================="
echo "开始批量测试矩阵运算 - 断点续测模式"
echo "测试程序: $executable"
echo "测试矩阵大小: ${sizes[@]}"
echo "总组合数: $total_combinations"
echo "已完成测试数: $completed_tests"
echo "剩余测试数: $((total_combinations - completed_tests))"
echo "从以下组合继续: M=$RESUME_M, N=$RESUME_N, K=$RESUME_K 的下一个"
echo "========================================="
echo

# 初始化计数器
current_test=$completed_tests
total_time=0
actual_tests=0
skip_mode=1  # 1表示跳过模式，0表示正常测试模式

# 三重循环遍历所有M、N、K组合
for m in "${sizes[@]}"; do
    for n in "${sizes[@]}"; do
        for k in "${sizes[@]}"; do
            # 检查是否到达断点位置
            if [ $skip_mode -eq 1 ]; then
                if [ $m -eq $RESUME_M ] && [ $n -eq $RESUME_N ] && [ $k -eq $RESUME_K ]; then
                    echo "已到达断点位置: M=$m, N=$n, K=$k"
                    echo "从下一个组合开始测试..."
                    skip_mode=0
                fi
                continue
            fi
            
            current_test=$((current_test + 1))
            actual_tests=$((actual_tests + 1))
            
            echo "----------------------------------------"
            echo "测试进度: [$current_test/$total_combinations] (本次运行: $actual_tests)"
            echo "正在测试矩阵大小: M=$m, N=$n, K=$k"
            echo "执行命令: $executable $m $n $k"
            echo "----------------------------------------"
            
            # 记录开始时间
            start_time=$(date +%s.%N)
            
            # 执行命令
            $executable $m $n $k
            execution_result=$?
            
            # 记录结束时间
            end_time=$(date +%s.%N)
            
            # 计算执行时间（精确到毫秒）
            execution_time=$(echo "$end_time - $start_time" | bc)
            total_time=$(echo "$total_time + $execution_time" | bc)
            
            # 检查执行结果
            if [ $execution_result -eq 0 ]; then
                echo "✓ 执行成功，耗时: ${execution_time} 秒"
            else
                echo "✗ 执行失败，返回码: $execution_result"
            fi
            
            # 估算剩余时间
            if [ $actual_tests -gt 0 ]; then
                avg_time=$(echo "scale=3; $total_time / $actual_tests" | bc)
                remaining_tests=$((total_combinations - current_test))
                estimated_remaining=$(echo "scale=1; $avg_time * $remaining_tests" | bc)
                echo "平均耗时: ${avg_time}s, 预计剩余时间: ${estimated_remaining}s"
            fi
            
            echo
            
            # 可选：添加短暂延迟避免系统过载
            # sleep 0.1
        done
    done
done

echo "========================================="
echo "测试完成！"
echo "本次运行测试数: $actual_tests"
echo "累计完成测试数: $current_test / $total_combinations"
echo "本次运行总耗时: ${total_time} 秒"
if [ $actual_tests -gt 0 ]; then
    echo "本次运行平均耗时: $(echo "scale=3; $total_time / $actual_tests" | bc) 秒/测试"
fi
echo "========================================="